#include "centralwidget.h"
#include "PlaceManger.h"
#include <QPainter>
#include <QDebug>
#include <QPixmap>
#include <QPaintEvent>
#include <QPen>
#include <QBrush>
#include <QColor>

CentralWidget::CentralWidget(QWidget *parent) : QWidget(parent)
{
    this->setStyleSheet("background-color:rgb(255,255,255)");
    resize(1100,750);
    this->x1 = -1;
    this->x2 = -1;
    this->y1 = -1;
    this->y2 = -1;
    this->type = 0;
    this->length1 = -1;
    this->length2 = 0;
    this->length3 = 0;
    this->space = 55;
    this->offsetX = (width()-17*space)/2;
    this->offsetY = 50;
    this->length3 = 0;
    strcpy(this->flagName,"");
}

void CentralWidget::setFlagName(char *name)
{
    strcpy(this->flagName,name);
    this->update();
}

void CentralWidget::paintEvent(QPaintEvent *)
{

    this->paintMap();
    if(type==1&&x1!=-1&&y1!=-1&&x2!=-1&&y2!=-1)
    {
        this->paintB();
        emit sendLength(length1 - 1);
    }
    if(type==2&&x1!=-1&&y1!=-1&&x2!=-1&&y2!=-1)
    {
        this->paintBus();
        this->paintMinBus();
        emit sendGJNode(path2,length2,path3,length3);
    }
    if(type==3&&x1!=-1&&y1!=-1&&x2!=-1&&y2!=-1)
    {
        if(length1==0)
            path = PM.BFS(x1,y1,x2,y2,length1);
        if(length2==0)
            path2 = PM.GJPlan(x1,y1,x2,y2,length2);
        //qDebug() <<"--------------";
        if(length1<length2)
            paintB();
        else
            paintBus();
        //换乘错误
        paintMinBus();

        emit sendAll(length1 - 1,path2,length2,path3,length3);
    }
    if(strcmp(this->flagName,"")!=0)
    {
        this->flagPlace(flagName);
    }
}

void CentralWidget::Planing(int x1, int y1, int x2, int y2)
{
    type = 1;
    this->length1 = 0;
    this->x1 = x1;
    this->x2 = x2;
    this->y1 = y1;
    this->y2 = y2;
    this->update();
    //qDebug() <<x1<<" "<<y1<<" "<<x2<<" "<<y2;
}

void CentralWidget::GJPlaning(int type, int x1, int y1, int x2, int y2)
{
    this->type = 2;
    this->length2 = 0;
    this->length3 = 0;
    this->x1 = x1;
    this->x2 = x2;
    this->y1 = y1;
    this->y2 = y2;
    this->update();
}

void CentralWidget::AllPlaning(int x1, int y1, int x2, int y2)
{
    type = 3;
    this->length1 = 0;
    this->length2 = 0;
    this->length3 = 0;
    this->x1 = x1;
    this->x2 = x2;
    this->y1 = y1;
    this->y2 = y2;
    this->update();
}
//标识
void CentralWidget::flagPlace(char *name)
{
    Place place;
    place = PM.getPalceByName(name);
    QPainter p(this);
    QFont font("微软雅黑",8);
    p.setFont(font);
    QPen pen(QColor("red"));
    p.setPen(pen);
    int width = 30;
    int hight = 30;
    int x = place.getX()*space + offsetX - width/2;
    int y = place.getY()*space + offsetY - hight;
    p.drawPixmap(x,y,width,hight,QPixmap(":/new/prefix1/Images/star2.png"));
    this->update();
}

void CentralWidget::paintMap()
{

    //地图初始化
    int sum = 0;
    Place *places = PM.List(sum);
    //qDebug()<<sum;
    QPainter p(this);
    //背景
    //this->setStyleSheet("background-color:white");
    //p.drawPixmap(0,0,width(),770,QPixmap(":/new/prefix1/Images/bg2.jpg"));
    QPen pen(QColor(254,214,105));
    pen.setWidth(3);
    p.setPen(pen);
    for(int i = 0;i<sum;i++)
    {
        pen.setColor(QColor(254,214,105));
        p.setPen(pen);
        if(places[i].getUp()==1)  //x y-1
        {
            int x1 = places[i].getX()*space + offsetX;
            int y1 = places[i].getY()*space + offsetY;
            int x2 = (places[i].getX())*space + offsetX;
            int y2 = (places[i].getY()-1)*space + offsetY;
            p.drawLine(x1,y1,x2,y2);
        }

        if(places[i].getDown()==1) //x y+1
        {
            int x1 = places[i].getX()*space + offsetX;
            int y1 = places[i].getY()*space + offsetY;
            int x2 = (places[i].getX())*space + offsetX;
            int y2 = (places[i].getY()+1)*space + offsetY;
            p.drawLine(x1,y1,x2,y2);
        }

        if(places[i].getLeft()==1) //x-1 y
        {
            int x1 = places[i].getX()*space + offsetX;
            int y1 = places[i].getY()*space + offsetY;
            int x2 = (places[i].getX()-1)*space + offsetX;
            int y2 = (places[i].getY())*space + offsetY;
            p.drawLine(x1,y1,x2,y2);
        }

        if(places[i].getRight()==1) //x+1 y
        {
            int x1 = places[i].getX()*space + offsetX;
            int y1 = places[i].getY()*space + offsetY;
            int x2 = (places[i].getX()+1)*space + offsetX;
            int y2 = (places[i].getY())*space + offsetY;
            p.drawLine(x1,y1,x2,y2);
        }

    }
    for(int i= 0;i<sum;i++)
    {
        int x = places[i].getX()*space+offsetX;
        int y = places[i].getY()*space+offsetY;
        if(strcmp(places[i].getName(),"")!=0)
        {
            QFont font("微软雅黑",8);
            p.setFont(font);
            pen.setColor(QColor("black"));
            if(strcmp(places[i].getName(),"")!=0)
            {
                p.setPen(pen);
                p.drawPixmap(x-10,y-12,20,20,QPixmap(":/new/prefix1/Images/star.png"));
                //p.drawText(x-10,y+15,QString::number(places[i].getBicycleNum()));
                if(QString(places[i].getName()).length()<=2)
                    p.drawText(x - 15,y + 15,QString(places[i].getName()));
                else
                    p.drawText(x - space/2,y + 15,QString(places[i].getName()));
                p.drawText(x - 15,y+30,QString("(") + QString::number(places[i].getBicycleNum()) + ")");
            }
        }
    }
}

void CentralWidget::paintB()
{
    int BOffsetX = 4;
    int BOFFsetY = 4;
    QPainter p(this);
    if(length1==0)
    {
        //delete[] path;
        path = PM.BFS(x1,y1,x2,y2,length1);
    }

    QPen pen(Qt::red);
    pen.setWidth(4);
    pen.setColor(QColor(160, 32, 240));
    p.setPen(pen);
    //qDebug() <<length;
    for(int i = 0;i<length1-1;i++)
    {

        int x1 = path[i].x*space + offsetX;
        int y1 = path[i].y*space + offsetY;
        int x2 = path[i+1].x*space + offsetX;
        int y2 = path[i+1].y*space + offsetY;
        int width = 30;
        int hight = 30;

        p.drawLine(x1 - BOffsetX,y1 - BOFFsetY,x2 - BOffsetX,y2 - BOFFsetY);
        if(i==0)
        {
            p.drawPixmap(x1 - width/2,y1 - hight,width,hight,QPixmap(":/new/prefix1/Images/end.png"));
        }
        if(i==length1-2)
        {
            p.drawPixmap(x2 - width/2,y2 - hight,width,hight,QPixmap(":/new/prefix1/Images/begin.png"));
        }
        //qDebug() <<path[i].x <<" "<<path[i].y;
    }
}

void CentralWidget::paintBus()
{
    int BusOffsetX = 4;
    int BusOffsetY = 4;
    //qDebug()<<"+++++++++++++";
    QPainter p(this);
    int changeColor = 0;
    if(length2==0)
        path2 =  PM.GJPlan(x1,y1,x2,y2,length2);
    QPen pen(Qt::green);
    pen.setWidth(4);

    //qDebug() <<length;
    for(int i = 0;i<length2-1;i++)
    {

        if(path2[i].status==0&&i!=0)
        {
            if(changeColor==0)
            {
                pen.setColor(QColor(139, 69, 0));
                changeColor++;
            }
            else if(changeColor==1)
            {
                pen.setColor(Qt::red);
                changeColor++;
            }
            else if(changeColor==2)
            {
                pen.setColor(Qt::blue);
                changeColor++;
            }
        }
        p.setPen(pen);
        this->update();
        int x1 = path2[i].x*space + offsetX;
        int y1 = path2[i].y*space + offsetY;
        int x2 = path2[i+1].x*space + offsetX;
        int y2 = path2[i+1].y*space + offsetY;

        p.drawLine(x1 + BusOffsetX,y1 + BusOffsetY,x2 + BusOffsetX,y2 + BusOffsetY);
        int width = 30;
        int hight = 30;
        if(i==0)
        {
            p.drawPixmap(x1 - width/2,y1 - hight,width,hight,QPixmap(":/new/prefix1/Images/begin.png"));
        }
        if(i==length2-2)
        {
            p.drawPixmap(x2 - width/2,y2 - hight,width,hight,QPixmap(":/new/prefix1/Images/end.png"));
        }
        //qDebug() <<path[i].x <<" "<<path[i].y;

    }
    //emit sendLength(length - 1);
}

void CentralWidget::paintMinBus()
{
    int BusOffsetX = 4;
    int BusOffsetY = 4;
    //qDebug()<<"+++++++++++++";
    QPainter p(this);
    int changeColor = 0;
    if(length3==0)
        path3 =  PM.GJMinStopPlan(x1,y1,x2,y2,length3);
    QPen pen(Qt::green);
    pen.setWidth(4);

    //qDebug() <<length;
    for(int i = 0;i<length3-1;i++)
    {
        if(path3[i].status==0&&i!=0)
        {
            if(changeColor==0)
            {
                pen.setColor(QColor(139, 69, 0));
                changeColor++;
            }
            else if(changeColor==1)
            {
                pen.setColor(Qt::red);
                changeColor++;
            }
            else if(changeColor==2)
            {
                pen.setColor(Qt::blue);
                changeColor++;
            }
        }
        p.setPen(pen);
        this->update();
        int x1 = path3[i].x*space + offsetX;
        int y1 = path3[i].y*space + offsetY;
        int x2 = path3[i+1].x*space + offsetX;
        int y2 = path3[i+1].y*space + offsetY;
        p.drawLine(x1 + BusOffsetX,y1 + BusOffsetY,x2 + BusOffsetX,y2 + BusOffsetY);
        int width = 30;
        int hight = 30;
        if(i==0)
        {
            p.drawPixmap(x1 - width/2,y1 - hight,width,hight,QPixmap(":/new/prefix1/Images/begin.png"));
        }
        if(i==length3-2)
        {
            p.drawPixmap(x2 - width/2,y2 - hight,width,hight,QPixmap(":/new/prefix1/Images/end.png"));
        }
        //qDebug() <<path[i].x <<" "<<path[i].y;
    }
}
